package com.kefu.call.controller;

import com.kefu.common.mybatis.Order;
import com.kefu.sys.service.SysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.call.model.CdrCustomer;
import com.kefu.call.service.CdrCustomerService;
import com.kefu.call.vo.CdrCustomerSearchVo;
import com.kefu.call.vo.CdrCustomerInsertVo;
import com.kefu.call.vo.CdrCustomerUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Optional;

@Api(tags = "计费_计费客户")
@RestController
@RequestMapping("call/cdr_customer")
public class CdrCustomerController extends AbstractBaseController {

    @Autowired
    private CdrCustomerService cdrCustomerService;
    @Autowired
    SysTenantService sysTenantService;

//    @PostMapping("/add")
//    @ApiOperation(value = "新增计费_计费客户", notes = "新增计费_计费客户")
//    public Result<String> add(@Validated @RequestBody CdrCustomerInsertVo cdrCustomerVo) {
//    	logger.info("新增计费_计费客户:{}", cdrCustomerVo);
//
//    	CdrCustomer info = cdrCustomerVo.convert();
//        cdrCustomerService.save(info);
//        return ResultGenerator.genSuccessResult();
//    }

//    @PostMapping("/delete")
//    @ApiOperation(value = "删除计费_计费客户", notes = "删除计费_计费客户")
//    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
//    	logger.info("删除计费_计费客户:{}", delete);
//
//        cdrCustomerService.deleteById(delete.getId());
//        return ResultGenerator.genSuccessResult();
//    }

    @PostMapping("/update")
    @ApiOperation(value = "修改计费_计费客户", notes = "修改计费_计费客户")
    public Result<String> update(@Validated @RequestBody CdrCustomerUpdateVo cdrCustomerVo) {
    	logger.info("修改计费_计费客户:{}", cdrCustomerVo);
    	
    	CdrCustomer info = cdrCustomerVo.convert();
        cdrCustomerService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "计费_计费客户详情", notes = "计费_计费客户详情")
    public Result<CdrCustomer> detail(@Validated @RequestBody DetailVo detail) {

        CdrCustomer cdrCustomer = cdrCustomerService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(cdrCustomer);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询计费_计费客户", notes = "查询计费_计费客户")
    public Result<Page<CdrCustomer>> list(@Validated @RequestBody CdrCustomerSearchVo search) {

        search.setOrderField("oid");
        search.setOrder(Order.DESC);
        Page<CdrCustomer> page = cdrCustomerService.searchLambdaPage(search);
        page.getRecords().forEach(item -> Optional.ofNullable(item.getCompany()).map(i->sysTenantService.getById(i)).ifPresent(i->item.setName(i.getTenantName())));
        return ResultGenerator.genSuccessResult(page);
    }
}
